package ${basePackageUrl}.controller;

import ${basePackageUrl}.api.CommonResult;
import ${basePackageUrl}.service.RedisService;
import ${basePackageUrl}.domain.${entityName};
import ${basePackageUrl}.dto.${entityName}Query;
import ${basePackageUrl}.service.${entityName}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.lang.reflect.Field;
import java.util.Date;

@Controller
@Api(tags = "${entityName}Controller", description = "${tableComment}")
@RequestMapping("/${contextUri}")
public class ${entityName}Controller
{

    @Autowired
    ${entityName}Service ${contextUri}Service;
    @Autowired
    RedisService redisService;

    @ApiOperation(value = "${tableComment}列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<${entityName}>> list(${entityName}Query query) {

        try {
            Page<${entityName}> pageData = ${contextUri}Service.findAll(query);
            return CommonResult.success(pageData);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @ApiOperation(value = "新增${tableComment}")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody ${entityName} ${contextUri}) {
        ${contextUri}.setId(${contextUri}Service.createId(${contextUri}));

        ${contextUri}.setCreateTime(new Date());
        ${contextUri}.setUpdateTime(new Date());

        ${contextUri}Service.save(${contextUri});
        return CommonResult.success();
    }

    @ApiOperation(value = "修改${tableComment}")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody ${entityName} ${contextUri}) {

        ${contextUri}.setUpdateTime(new Date());

        ${contextUri}Service.save(${contextUri});
        return CommonResult.success();
    }

    @ApiOperation(value = "删除${tableComment}")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult delete(@RequestBody ${entityName} ${contextUri}) {
        ${contextUri}Service.deleteById(${contextUri}.getId());
        return CommonResult.success();
    }

}
